<?php

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modeladmin');


require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/appcategory.php';

class AppcategorysModelAppcategory extends JModelAdmin {

    protected $text_prefix = 'COM_APPCATEGORYS';

    protected function canDelete($record) {
        if (!empty($record->id)) {
            if ($record->state != -2) {
                return;
            }
            $user = JFactory::getUser();
            return $user->authorise('core.delete', 'com_appcategorys.appcategorys.' . (int) $record->id);
        }
    }

    protected function canEditState($record) {
        $user = JFactory::getUser();

        // Check for existing article.
        if (!empty($record->id)) {
            return $user->authorise('core.edit.state', 'com_appcategorys.appcategorys.' . (int) $record->id);
        }
        // New article, so check against the category.
        elseif (!empty($record->catid)) {
            //Jerror::raiseWarning(100, $user->authorise('core.edit.state', 'com_appcategorys.appcategory.' . (int) $record->catid));
            return $user->authorise('core.edit.state', 'com_appcategorys.appcategorys.' . (int) $record->catid);
        }
        // Default to component settings if neither article nor category known.
        else {
            return parent::canEditState('com_appcategorys');
        }
    }

    public function getTable($type = 'Appcategorys', $prefix = 'JTable', $config = array()) {
        return JTable::getInstance($type, $prefix, $config);
    }

    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_appcategorys.appcategory', 'appcategory', array('control' => 'jform', 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        return $form;
    }

    protected function loadFormData() {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState('com_appcategorys.edit.appcategory.data', array());

        if (empty($data)) {
            $data = $this->getItem();
        
            // Prime some default values.
            if ($this->getState('appcategory.id') == 0) {
                $app = JFactory::getApplication();
                $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_appcategorys.appcategory.filter.category_id')));
            }
        }
        return $data;
    }

    public function save($data) {
        /* 	if (isset($data['images']) && is_array($data['images'])) {
          $registry = new JRegistry;
          $registry->loadArray($data['images']);
          $data['images'] = (string)$registry;

          }

          if (isset($data['urls']) && is_array($data['urls'])) {
          $registry = new JRegistry;
          $registry->loadArray($data['urls']);
          $data['urls'] = (string)$registry;

          }
          // Alter the title for save as copy
          if (JRequest::getVar('task') == 'save2copy') {
          list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
          $data['title']	= $title;
          $data['alias']	= $alias;
          }
         */
        JError::raiseWarning(100, "savw");
        //var_export($data);  
        if (parent::save($data)) {

            //	if (isset($data['featured'])) {
            //		$this->featured($this->getState($this->getName().'.id'), $data['featured']);
            //	}

             //   JError::raiseWarning(100, "Da vao da2222y");
            return true;
        }


        return false;
    }

}
